package servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import usecases.GestionApprentiPizzaiolo;
import domaine.ApprentiPizzaiolo;

/**
 * Servlet implementation class Enregistrer
 */
@WebServlet(name="sinscrire.html")
public class Sinscrire extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB
	private GestionApprentiPizzaiolo gestionApprentiPizzaiolo;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String login = request.getParameter("login");
		String nom = request.getParameter("nom");
		String prenom = request.getParameter("prenom");
		String mail = request.getParameter("mail");
		String mdp = request.getParameter("mdp");
		String mdpBis = request.getParameter("mdpBis");

		if (!mdp.equals(mdpBis)) {
			RequestDispatcher rd = getServletContext().getNamedDispatcher(
					"inscription.html");
			rd.forward(request, response);
			return;
		}

		else {
			try {
				gestionApprentiPizzaiolo.sInscrire(new ApprentiPizzaiolo(login,
						nom, prenom, mail, mdp));
				RequestDispatcher rd = getServletContext().getNamedDispatcher(
						"seconnecter.html");
				rd.forward(request, response);
			} catch (Throwable e) {
				RequestDispatcher rd = getServletContext().getNamedDispatcher(
						"inscription.html");
				rd.forward(request, response);
				return;
			}
			
		}

	}

}
